package com.chixing.service.impl;

import com.chixing.mapper.VipMapper;
import com.chixing.pojo.Vip;
import com.chixing.pojo.VipExample;
import com.chixing.service.VipService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VipServiceImpl implements VipService {
    @Autowired
    private VipMapper vipMapper;
    @Override
    public ServerResponse getByCusId(Integer cusId) {
        VipExample example = new VipExample();
        example.createCriteria().andCusIdEqualTo(cusId);
        List<Vip> vipList = vipMapper.selectByExample(example);
        if (vipList !=null && vipList.size()>0) {
            Vip vip = vipList.get(0);
            return ServerResponse.getSuccess(vip);
        }else
            return ServerResponse.getFail(null);

    }

    @Override
    public ServerResponse save(Vip vip) {
        vip.setVipStartDate(new Date());
        int rows = vipMapper.insert(vip);
        if (rows>0)
            return ServerResponse.saveSuccess(rows);
        return ServerResponse.saveFail(null);
    }

    @Override
    public ServerResponse update(Vip vip) {
        vip.setVipStartDate(new Date());
        int rows = vipMapper.insert(vip);
        if (rows>0)
            return ServerResponse.updateSuccess(rows);
        return ServerResponse.updateFail(null);
    }

    @Override
    public ServerResponse getByVipId(Integer vipId) {
        Vip vip = vipMapper.selectByPrimaryKey(vipId);
        if (vip !=null)
            return ServerResponse.getSuccess(vip);
        return ServerResponse.getFail(null);
    }
}
